﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 
using System.Windows.Forms;

namespace JGSY.Common.ShareControls.Framework
{
    public partial class MeterCustomer : UserControl
    {
        public MeterCustomer()
        {
            this.InitializeComponent();

            base.SuspendLayout();
            base.AutoScaleMode = AutoScaleMode.Inherit;
            base.Margin = new Padding(3, 2, 3, 2);
            base.Name = "MeterCustomer";
            base.Size = new Size(165, 125);
            base.ResumeLayout(false);


            base.SetStyle(ControlStyles.Selectable, true);
            base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            base.SetStyle(ControlStyles.UserPaint, true);
            base.SetStyle(ControlStyles.ResizeRedraw, true);
            base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            base.Size = new Size(200, 200);
            this.rect = base.ClientRectangle;
        }
        public double Value
        {
            get
            {
                return this.d_value;
            }
            set
            {
                this.d_value = value;
                base.Invalidate();
            }
        }
        public int MinValue
        {
            get
            {
                return this.minValue;
            }
            set
            {
                this.minValue = value;
                base.Invalidate();
            }
        }

       
        public int MaxValue
        {
            get
            {
                return this.maxValue;
            }
            set
            {
                this.maxValue = value;
                base.Invalidate();
            }
        }

    
        public string Unit
        {
            get
            {
                return this.unit;
            }
            set
            {
                this.unit = value;
                base.Invalidate();
            }
        }
         
        public string Header
        {
            get
            {
                return this.header;
            }
            set
            {
                this.header = value;
                base.Invalidate();
            }
        }

       
        public Color CenterColor
        {
            get
            {
                return this.centerColor;
            }
            set
            {
                this.centerColor = value;
                base.Invalidate();
            }
        }

     
        public Color SurroundColor
        {
            get
            {
                return this.surroundColor;
            }
            set
            {
                this.surroundColor = value;
                base.Invalidate();
            }
        }
 
        public Color ScaleLineColor
        {
            get
            {
                return this.scaleLineColor;
            }
            set
            {
                this.scaleLineColor = value;
                base.Invalidate();
            }
        }

        
        public Color ScaleValueColor
        {
            get
            {
                return this.scaleValueColor;
            }
            set
            {
                this.scaleValueColor = value;
                base.Invalidate();
            }
        }

       
        public Color PointColor
        {
            get
            {
                return this.pointColor;
            }
            set
            {
                this.pointColor = value;
                base.Invalidate();
            }
        }

      
        public Color UnitColor
        {
            get
            {
                return this.unitColor;
            }
            set
            {
                this.unitColor = value;
                base.Invalidate();
            }
        }
 
        public Color HeaderColor
        {
            get
            {
                return this.headerColor;
            }
            set
            {
                this.headerColor = value;
                base.Invalidate();
            }
        }

        public Color ValueColor
        {
            get
            {
                return this.valueColor;
            }
            set
            {
                this.valueColor = value;
                base.Invalidate();
            }
        }

        public Color CenterElipseColor
        {
            get
            {
                return this.centerElipseColor;
            }
            set
            {
                this.centerElipseColor = value;
                base.Invalidate();
            }
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            base.Height = base.Width;
            this.rect = base.ClientRectangle;
            base.Region = new Region(this.rect);
            this.rect.Width = this.rect.Width - 1;
            this.rect.Height = this.rect.Height - 1;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            this.DrawMeter(e, this.Value, this.MinValue, this.MaxValue, this.Unit, this.Header);
        }

        private void DrawMeter(PaintEventArgs e, double value, int min, int max, string unit, string header)
        {
            Graphics graphics = e.Graphics;
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            int num = Math.Min(this.rect.Width, this.rect.Height);
            double num2 = (double)num * 1.0 / 2.0;
            Point point = new Point(this.rect.Width / 2, this.rect.Height / 2);
            GraphicsPath graphicsPath = new GraphicsPath();
            graphicsPath.AddEllipse(this.rect);
            PathGradientBrush pathGradientBrush = new PathGradientBrush(graphicsPath);
            pathGradientBrush.CenterColor = this.CenterColor;
            Color[] surroundColors = new Color[]
            {
                this.SurroundColor
            };
            pathGradientBrush.SurroundColors = surroundColors;
            graphics.FillEllipse(pathGradientBrush, this.rect);
            int num3 = 10;
            int num4 = min;
            for (int i = 0; i <= 100; i++)
            {
                int num5 = 6;
                bool flag = i % 10 == 0;
                if (flag)
                {
                    num5 = 10;
                }
                int x = (int)((double)point.X + num2 * Math.Cos(((double)i * 2.7 + 135.0) * 3.1415926535897931 / 180.0));
                int y = (int)((double)point.Y + num2 * Math.Sin(((double)i * 2.7 + 135.0) * 3.1415926535897931 / 180.0));
                int x2 = (int)((double)point.X + (num2 - (double)num5) * Math.Cos(((double)i * 2.7 + 135.0) * 3.1415926535897931 / 180.0));
                int y2 = (int)((double)point.Y + (num2 - (double)num5) * Math.Sin(((double)i * 2.7 + 135.0) * 3.1415926535897931 / 180.0));
                graphics.DrawLine(new Pen(this.ScaleLineColor), new Point(x, y), new Point(x2, y2));
                bool flag2 = i % 10 == 0;
                if (flag2)
                {
                    int x3 = (int)((double)point.X + (num2 - 22.0) * Math.Cos(((double)i * 2.7 + 135.0) * 3.1415926535897931 / 180.0)) - 8;
                    int y3 = (int)((double)point.Y + (num2 - 22.0) * Math.Sin(((double)i * 2.7 + 135.0) * 3.1415926535897931 / 180.0)) - 8;
                    graphics.DrawString(num4.ToString(), new Font("Microsoft YaHei", 8f), new SolidBrush(this.ScaleValueColor), new Point(x3, y3));
                    num4 += (max - min) / num3;
                }
            }
            int num6 = (int)((value - (double)min) * 2.7 + 135.0);
            int num7 = num6 - 90;
            int num8 = num6 + 90;
            int x4 = (int)((double)point.X + (num2 - 5.0) * Math.Cos((double)num6 * 3.1415926535897931 / 180.0));
            int y4 = (int)((double)point.X + (num2 - 5.0) * Math.Sin((double)num6 * 3.1415926535897931 / 180.0));
            int x5 = (int)((double)point.X + 3.0 * Math.Cos((double)num7 * 3.1415926535897931 / 180.0));
            int y5 = (int)((double)point.Y + 3.0 * Math.Sin((double)num7 * 3.1415926535897931 / 180.0));
            int x6 = (int)((double)point.X + 3.0 * Math.Cos((double)num8 * 3.1415926535897931 / 180.0));
            int y6 = (int)((double)point.Y + 3.0 * Math.Sin((double)num8 * 3.1415926535897931 / 180.0));
            List<Point> list = new List<Point>
            {
                new Point(x5, y5),
                new Point(x4, y4),
                new Point(x6, y6)
            };
            graphics.FillPolygon(new SolidBrush(this.PointColor), list.ToArray());
            graphics.FillEllipse(new SolidBrush(this.CenterElipseColor), point.X - 25, point.Y - 25, 50, 50);
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            graphics.DrawString(unit, new Font("Microsoft YaHei", 8f, FontStyle.Bold), new SolidBrush(this.UnitColor), new Point(point.X, this.rect.Height - 50), stringFormat);
            graphics.DrawString(header, new Font("Microsoft YaHei", 8f), new SolidBrush(this.HeaderColor), new Point(point.X, this.rect.Height - 20), stringFormat);
            graphics.DrawString(value.ToString("00.#"), new Font("Microsoft YaHei", 13f), new SolidBrush(this.ValueColor), new Point(point.X + 1, point.Y - 12), stringFormat);
        }

    

        private double d_value = 0.0;

        private int minValue = 0;

        private int maxValue = 50;

        private string unit = "℃";

        private string header = "Temperature";

        private Color centerColor = Color.FromArgb(90, 62, 155, 253);

        private Color surroundColor = Color.FromArgb(0, 33, 97, 216);

        private Color scaleLineColor = Color.Gray;

        private Color scaleValueColor = Color.Gray;

        private Color pointColor = Color.Red;

        private Color unitColor = ColorTranslator.FromHtml("#990146AE");

        private Color headerColor = ColorTranslator.FromHtml("#990146AE");

        private Color valueColor = Color.White;

        private Color centerElipseColor = ColorTranslator.FromHtml("#3E9BFC");

        private Rectangle rect;
         
    }
}
